#### EXERCICIO 7- REGRESSO LINEAR SIMPLES -OLIDAN POCIUS
## ALTURA INFANCIA E VIDA ADULTA
ALT.INF<-c(39,30,32,34,35,36,36,30)
ALT.ADU<-c(71,63,63,67,68,68,70,64)
ALT.ADU.CRE<-2*(ALT.INF)
# Q1
modelo1<-lm(ALT.ADU~ALT.INF)
summary(modelo1)
#Residual standard error: 1.091 on 6 degrees of freedom
#Multiple R-squared: 0.8942,  Adjusted R-squared: 0.8765 
#F-statistic:  50.7 on 1 and 6 DF,  p-value: 0.000386 ## sIM, EXISTE UMA RELAAO SIGNIFICATIVA QUE EXPLICA 87% DA VARIAAO
par(mfrow=c(2,2))
plot(modelo1)
par(mfrow=c(1,1))
#Q2
modelo2<-lm(ALT.ADU.CRE~ALT.INF)
summary(modelo2)
par(mfrow=c(2,2))
plot(modelo2)
par(mfrow=c(1,1))
plot(ALT.ADU~ALT.INF)
abline(modelo1)
abline(modelo2,col="red")
#Q3
confint(modelo1)
#                2.5 %    97.5 %
#(Intercept) 24.2881165 46.069026
#ALT.INF      0.6094693  1.247674
## O valor 2 (valor da crena) no est contido no intervalo de confiana

### Seriemas e Carcars
AVES<-read.table("aves_cerrado.csv",header=TRUE,sep=";")
AVES$fisionomia[AVES$fisionomia=="ce"]<-"Ce"
AVES<-AVES[apply(is.na(AVES),1,sum)==0,]
## Q1 E Q2

C.Limpo<-lm(carcara~seriema,subset=fisionomia=="CL",data=AVES)
summary(C.Limpo)
## Multiple R-squared: 0.2851,  Adjusted R-squared: 0.2431 
## F-statistic:  6.78 on 1 and 17 DF,  p-value: 0.01853 ###### SIGNIFICATIVO
confint(C.Limpo)
#                 2.5 %      97.5 %
#(Intercept)  4.9056072  8.27483358
#seriema     -0.6295969 -0.06599542

C.Cerrado<-lm(carcara~seriema,subset=fisionomia=="CC",data=AVES)
summary(C.Cerrado)
##Multiple R-squared: 0.02221,  Adjusted R-squared: -0.03531 
##F-statistic: 0.3862 on 1 and 17 DF,  p-value: 0.5426 
confint(C.Cerrado)
#                 2.5 %     97.5 %
#(Intercept)  7.5968111 15.9361838
#seriema     -0.9872355  0.5379969

C.estrito<-lm(carcara~seriema,subset=fisionomia=="Ce",data=AVES)
summary(C.estrito)
##Multiple R-squared: 0.02735,  Adjusted R-squared: -0.02987 
##F-statistic: 0.478 on 1 and 17 DF,  p-value: 0.4987 
confint(C.estrito)
#               2.5 %     97.5 %
#(Intercept) 10.612756 23.3103207
#seriema     -1.480390  0.7496205

## HOUVE RELAAO SIGNIFICATIVA ENTRE AVISTAMENTOS DE SERIEMAS E CARACARAS SOMENTE PARA CAMPO LIMPO
## OS INTERVALOS DE CONFIANA PARA OS COEFICIENTES SO BASTANTE AMPLOS E SOBREPOSTOS PARA
## CAMPO CERRADO E CERRADO ESTRITO, SENDO QUE O INTERVALO PARA CAMPO LIMPO TEM MENOR
## AMPLITUDE E EST CONTIDO NOS ANTERIORES.

############### Resduos de Iris
library(datasets)
iris
setosa<-iris[iris$Species=="setosa",]
setosa
##Q1
plot(Sepal.Width~Sepal.Length, data=setosa)
LARG.COMP<-lm(Sepal.Width~Sepal.Length, data=setosa) 
summary(LARG.COMP)
#             Estimate Std. Error t value Pr(>|t|)    
#(Intercept)   -0.5694     0.5217  -1.091    0.281  ## INTERCEPTO nO SIG> A 5%  
#Sepal.Length   0.7985     0.1040   7.681 6.71e-10 *** ### RELAAO POSITIVA E SIGNIFICATIVA A 5%
#---
#Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
#
#Residual standard error: 0.2565 on 48 degrees of freedom
#Multiple R-squared: 0.5514,  Adjusted R-squared: 0.542 ### O COMPR. DA SPALA EXPLICA 54% da variaao da largura da spala
#F-statistic: 58.99 on 1 and 48 DF,  p-value: 6.71e-10 
par(mfrow=c(2,2))
plot(LARG.COMP)
par(mfrow=c(1,1))
## Q2
residuocomp<-residuals(lm(Sepal.Length~Petal.Length,data=setosa))
residuolarg<-residuals(lm(Sepal.Width~Petal.Length,data=setosa))
plot(residuolarg~residuocomp)
RES<-lm(residuolarg~residuocomp)
summary(RES)
#             Estimate Std. Error t value Pr(>|t|)    
#(Intercept) 1.407e-17  3.626e-02   0.000        1    ## intercepto comtinua no significativo
#residuocomp 8.049e-01  1.078e-01   7.465 1.43e-09 ***### RELAAO POSITIVA E SIG. a 5%, somente o valor do coef. diminuiu
#---
#Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
#
#Residual standard error: 0.2564 on 48 degrees of freedom
#Multiple R-squared: 0.5372,  Adjusted R-squared: 0.5276 ## RESULTADO SEMELHANTE, 52% da variaao explicada
#F-statistic: 55.72 on 1 and 48 DF,  p-value: 1.431e-09